package com.unitymarvel.harmonyos.projects.findme.slice;

import com.unitymarvel.harmonyos.projects.findme.MainAbility;
import com.unitymarvel.harmonyos.projects.findme.ResourceTable;
import com.unitymarvel.harmonyos.projects.findme.common.GameRecord;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import java.util.List;
///////////////////////////////////////////////////
// 项目的详细实现过程：https://ke.qq.com/course/3677276?tuin=a22a65ce
// 关注微信公众号和B站，获取更多鸿蒙学习资源
// 微信公众号：极客起源
// B站：https://space.bilibili.com/477001733
//
// 作者:李宁（蒙娜丽宁）
//
// 版本: 1.0
// Copyright (C), 2001-2022, 李宁
// 程序被版权法保护
//
///////////////////////////////////////////////////
public class GameRecordsAbilitySlice extends AbilitySlice {
    private List<GameRecord> gameRecords;
    private int[][] backgroundColors;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_game_records);
        gameRecords = MainAbility.dataService.getGameRecords();

        backgroundColors = new int[2][3];
        backgroundColors[0] = new int[]{255,192,203};
        backgroundColors[1] = new int[]{175,238,238};

        ListContainer listContainer = (ListContainer)findComponentById(ResourceTable.Id_list_container_gamerecords);
        listContainer.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return gameRecords.size();
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                DirectionalLayout view = (DirectionalLayout)LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_game_record, null, false);
                ShapeElement element = new ShapeElement();
                int[] backgroundColor = backgroundColors[i % 2];
                element.setRgbColor(new RgbColor(backgroundColor[0],backgroundColor[1], backgroundColor[2]));
                view.setBackground(element);

                Text textUser = (Text)view.findComponentById(ResourceTable.Id_text_user);
                Text textScore = (Text)view.findComponentById(ResourceTable.Id_text_score);
                Text textTime = (Text)view.findComponentById(ResourceTable.Id_text_time);

                textUser.setText("用户：" + gameRecords.get(i).user);
                textScore.setText("分数：" + gameRecords.get(i).score);
                textTime.setText("时间：" + gameRecords.get(i).time);
                return view;
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
